module.exports = function ($scope, $resource, $modalInstance, getDate, statue, item, toaster) {

	$scope.date = { 'lable': '', 'opened': false };
	$scope.date1 = { 'lable': '', 'opened': false };
	$scope.formdata = {
		'statue': statue,
		'price': 0,
		'hour1': 0,
		'hour2': 0,
		'minute1': 0,
		'minute2': 0,
		'stage_list': []
	}
	// item存在表示查看或修改
	if(item){
		$scope.formdata.id = item.id;
		$scope.formdata.price = item.price;
		$scope.formdata.market_price = item.market_price;
		$scope.formdata.sale_name = item.sale_name;
		$scope.formdata.sale_code = item.sale_code;
		$scope.formdata.hour1 = parseInt(item.start_time.substring(11,13));
		$scope.formdata.hour2 = parseInt(item.end_time.substring(11,13));
		$scope.formdata.minute1 = parseInt(item.start_time.substring(14,16));
		$scope.formdata.minute2 = parseInt(item.end_time.substring(14,16));
		$scope.date.lable = item.start_time.substring(0,10);
		$scope.date1.lable = item.end_time.substring(0,10);
		$resource('/api/as/tc/assemblesale/getSaleSectionList', {}, {}).save({'active_id':item.active_id}, function(res){
			if(res.errcode === 0){
				$scope.formdata.stage_list = res.data;
			}
		});
	}

	$scope.getSaleList = function (){
		var param = {
			'pageNo': 1,
			'pageSize': 20000,
			'state': '1'
		}
		// 查询全部已经上架的自营产品
		$resource('/api/ac/tc/ticketSaleService/getSaleList', {}, {}).save(param, function(res){
			if(res.errcode === 0){
				$scope.saleList = res.data.results;
				$scope.getSale2List($scope.saleList)
			} else {
				toaster.error({title: '', body: '查询产品列表失败'});
			}
		});
	}
	$scope.getSale2List = function (list){
		var param = {
			'pageNo': 1,
			'pageSize': 20000,
			'have': '1'
		}
		// 查询全部已经上架的自营产品
		$resource('/api/ac/tc/ticketSaleAuthService/findAllSaleList', {}, {}).save(param, function(res){
			if(res.errcode === 0){
				list.push.apply(list,res.data.results);
			} else {
				toaster.error({title: '', body: '查询产品列表失败'});
			}
		});
	}
	// 只有添加新活动产品时，需要查询产品列表
	if(statue == 'new'){
		$scope.getSaleList();
	}

	//打开日历空间,选择时间
    $scope.dateOpen = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
	};
	
	$scope.ok = function () {
		$scope.formdata.sale_code = $scope.formdata.item.code;
		$scope.formdata.sale_name = $scope.formdata.item.name;
		$scope.formdata.price = $scope.formdata.item.cost_price;
		$scope.formdata.market_price = $scope.formdata.item.market_price;
	}

	$scope.add = function () {
		$scope.formdata.stage_list.push({ 'end_num': 0, 'price': 0 });
	}
	$scope.remove = function (index) {
		$scope.formdata.stage_list.splice(index, 1);
	}
	
	$scope.save = function() {
		var param = {
			'start_time': getDate($scope.date.lable),
			'end_time': getDate($scope.date1.lable)
		}
		if(!$scope.date.lable || !$scope.date1.lable){
			toaster.warning({title: '参数错误', body: '未选择活动的开始或结束时间'});
			return;
		}
		if($scope.date.lable > $scope.date1.lable){
			toaster.warning({title: '参数错误', body: '活动的开始时间不能晚于结束时间'});
			return;
		} else if(param.start_time == param.end_time){
			var time1 = $scope.formdata.hour1 * 60 + $scope.formdata.minute1;
			var time2 = $scope.formdata.hour2 * 60 + $scope.formdata.minute2;
			if(time1 >= time2){
				toaster.warning({title: '参数错误', body: '活动的开始时间不能晚于结束时间'});
				return;
			}
		}
		if(!$scope.formdata.sale_code){
			toaster.warning({title: '参数错误', body: '请选择参加活动的产品'});
			return;
		}
		if(!$scope.formdata.stage_list || $scope.formdata.stage_list.length == 0){
			toaster.warning({title: '参数错误', body: '请填写产品阶段价格信息'});
			return;
		}
		if($scope.formdata.hour1 < 0 || $scope.formdata.hour2 < 0
			|| $scope.formdata.hour1 > 23 || $scope.formdata.hour2 > 23){
			toaster.warning({title: '参数错误', body: '请选择正确的时间范围'});
			return;
		}
		if($scope.formdata.minute1 < 0 || $scope.formdata.minute2 < 0
			|| $scope.formdata.minute1 > 59 || $scope.formdata.minute2 > 59){
			toaster.warning({title: '参数错误', body: '请选择正确的时间范围'});
			return;
		}
		var minNum = 0;
		for(var i = 0; i < $scope.formdata.stage_list.length; i++) {
			var temp = $scope.formdata.stage_list[i];
			if(!temp.price || temp.price < 0){
				toaster.warning({title: '参数错误', body: '活动产品阶段性价格错误'});
				return;
			}
			if(minNum >= temp.end_num){
				toaster.warning({title: '参数错误', body: '阶段人数错误，人数只可递增'});
				return;
			}
			minNum = temp.end_num;
		}
		if($scope.formdata.id) param.id = $scope.formdata.id;
		param.sale_code = $scope.formdata.sale_code;
		param.sale_name = $scope.formdata.sale_name;
		param.price = $scope.formdata.price;
		param.stage_list = $scope.formdata.stage_list;
		param.start_time = param.start_time+' '+$scope.formdata.hour1+':'+$scope.formdata.minute1+':00';
		param.end_time = param.end_time+' '+$scope.formdata.hour2+':'+$scope.formdata.minute2+':00';

		$resource('/api/ac/tc/assembleOrderService/saveAssembleSale', {}, {}).save(param, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '成功'});
				$modalInstance.close();
			} else {
				toaster.error({title: '', body: '失败'});
			}
		});
	}

	$scope.cancel = function() {
        $modalInstance.dismiss('cancel'); 
	};

};